﻿import SiteMapManage.SiteMapNode;

dynamic class SiteMapManage.SiteMapM{
	private var $nodeLink:String;
	private var $delLink:String;
	private var $editLink:String;
	private var $addChildLink:String;
	private var $editPanelLink:String;
	
	private var $nodeLevel:Number;
	private var $rootMovieClip:MovieClip;
	
	private var $rootnode:SiteMapManage.SiteMapNode;
	
	private var $nodeBTNXOffset:Number=2;
	private var $nodeBTNYOffset:Number=-25;
	
	private var $nodeBTNPXL:Number=2;
	
	private var $nodeXOffset:Number=10;
	private var $nodeYOffset:Number=30;
	
	private var $sitenodes:Array;
	private var $sitelineMc:MovieClip;
	
	private var $btnEnabled:Boolean;
	
	
	//是否为只读
	private var $readonly:Boolean=false;
	//////////////////////////////////////////////////////////////// 
	public function get ReadOnly():Boolean{
		return this.$readonly;
	}
	public function get BtnEnable():Boolean{
		return this.$btnEnabled;
	}
	public function set BtnEnable(value:Boolean){
		this.$btnEnabled=value;
	}
	public function get RootNode():SiteMapNode{
		return this.$rootnode;
	}
	public function get SiteLineMc():MovieClip{
		return this.$sitelineMc;
	}
	public function get NodeXOffset():Number{
		return this.$nodeXOffset;
	}
	public function get NodeBTNXOffset():Number{
		return this.$nodeBTNXOffset
	}
	public function get NodeBTNYOffset():Number{
		return this.$nodeBTNYOffset
	}
	public function get NodeBTNPXL():Number{
		return this.$nodeBTNPXL;
	}
	public function get NodeLevel():Number{
		return this.$nodeLevel++;
	}
	public function get RootMC():MovieClip{
		return this.$rootMovieClip;
	}
	
	public function get NodeLink():String{
		return this.$nodeLink;
	}
	public function get EditLink():String{
		return this.$editLink;
	}
	public function get AddLink():String{
		return this.$addChildLink;
	}
	public function get DelLink():String{
		return this.$delLink;
	}
	public function get EditPanelLink():String{
		return this.$editPanelLink;
	}
	////////////////////////////////////////////////////////////////
	public function SiteMapM(root:MovieClip){
		if(root==undefined){
			this.Say("rootMovieClip empty error!");
		}else{
			this.$rootMovieClip=root;
			initvars();
		}
	}
	private function initvars(){
		this.$nodeLevel=2000;
		this.$btnEnabled=true;
		this.$sitelineMc=$rootMovieClip.createEmptyMovieClip("drawLineMc",1000);
	}
	public function BindLinks(nodeL:String,delL:String,editL:String,addCL:String,editPanell){
		if(this.checkstringEorN(nodeL) && this.checkstringEorN(delL) && this.checkstringEorN(editL) && this.checkstringEorN(addCL) && this.checkstringEorN(editPanell)){
			this.$nodeLink=nodeL;
			this.$delLink=delL;
			this.$editLink=editL;
			this.$addChildLink=addCL;
			this.$editPanelLink=editPanell;
		}else{
			Say("Bind error!");
		}
	}
	public function CreateSiteMap(){
		if(check()){
			if(this.$rootnode==undefined){
				this.$rootnode=new SiteMapNode("root",this,0);
				this.$rootnode.NodeURL="http://";
				this.$rootnode.CreateNode();
			}else{
				createAllNodes(this.$rootnode);
			}
		}else{
			Say("error: not bind links");
		}
	}
	public function getSiteMapObj(obj:Object,node:SiteMapNode,checkObj:Object){
		if(obj==undefined) return;
		if(checkObj==undefined){
			checkObj=new Object();
		}
		if(node==undefined) {
			node=this.$rootnode;
		}
		if(checkObj[node.NodeURL]!=undefined){
			//obj=new Object();
			obj.errorMsg="节点["+checkObj[node.NodeURL]+"]与节点["+node.NodeName+"]的URL不能相同."
			trace(obj.errorMsg);
			return obj;
		}else{
			checkObj[node.NodeURL]=node.NodeName;
		}
		obj.name=node.NodeName;
		if(node.Parent!=undefined){
			//obj.father_id=node.Parent.NodeId;
			obj.father_id=node.Parent.NodeURL;
		}else{
			obj.father_id="-1"
		}
		obj.url=node.NodeURL;
		//bj.id=node.NodeId;
		obj.id=node.NodeURL;
		obj.child=new Array();
		if(node.Child.length>0){
			for(var i in node.Child){
				var subObj:Object=new Object();
				getSiteMapObj(subObj,node.Child[i],checkObj);
				if(subObj.errorMsg!=undefined){
					obj.errorMsg=subObj.errorMsg;
					return subObj;
				}
				obj.child.push(subObj);
			}
		}
	}
	public function initSiteNodes(arr:Array){
		
		if(arr.length>0){
			var tempArr:Array=new Array();
			while(arr.length>0){
				var nodeInfo:Object=arr.pop();
				//trace("\r\r-----------------------")
				//Debug.traceAllObj(nodeInfo);
				if(nodeInfo!=undefined){
					var nd:SiteMapNode=new SiteMapNode(nodeInfo.name,this,0);
					var added:Boolean=false;
					//nd.NodeId=nodeInfo.id;
					nd.NodeURL=nodeInfo.url;
					nd["pid"]=nodeInfo.father_id;
					//find father node
					if(nodeInfo.father_id==-1){
						trace(1)
						//tempArr.push(nd);
						//added=true;
					}else{
						
						for(var i=0;i<tempArr.length;i++){
							if(tempArr[i]!=null){
								var fnd:SiteMapNode=getNodeFromTree(tempArr[i],nodeInfo.father_id);
								if(fnd!=null){
									//trace("find father")
									fnd.AddNode(nd);
									added=true;
									break;
								}
							}
						}
					}
					trace("-----------"+tempArr)
					//find child node
					for(var i=0;i<tempArr.length;i++){
						if(tempArr[i]!=null){
							if(tempArr[i].pid==nodeInfo.id){
							//trace(3+":["+i+"]")
								nd.AddNode(tempArr[i]);
								tempArr[i]=null;
								if(!added){
									tempArr.push(nd);
									added=true;
								}
								//break;
							}
						}
					}
					if(!added){
						tempArr.push(nd);
					}
					//trace("-----------"+tempArr)
				}else{
					Say("node info error when initSiteNodes.nodeid="+nodeInfo.nodeid);
				}
			}
			//Debug.traceAllObj(tempArr);
			//_root.ttttttt= tempArr;
			for(var i=0;i<tempArr.length;i++){
				if(tempArr[i]!=null){
					//trace("root node id="+tempArr[i].NodeId);
					this.$rootnode=tempArr[i];
					break;
				}
			}
		}else{
			Say("no node inited!");
		}
	}
	private function getNodeFromTree(treeRootNode:SiteMapNode,id:Number):SiteMapNode{
		//trace("check node["+treeRootNode.NodeName+"]:"+treeRootNode.NodeId+" =? "+id)
		if(treeRootNode.NodeId!=id || treeRootNode==undefined) return null;
		if(treeRootNode.NodeId==id) return treeRootNode;
		for(var i=0;i<treeRootNode.Child.length;i++){
			var nd:SiteMapNode=getNodeFromTree(treeRootNode.Child[i],id);
			if(nd!=null)
			return nd;
		}
		return null;
	}
	private function createAllNodes(node:SiteMapNode){
		if(node==undefined) return;
		node.CreateNode();
		node.onRolloverNode=this.onRollOverFun;
		for(var i=0;i<node.Child.length;i++){
			createAllNodes(node.Child[i]);
		}
	}
	private function onRollOverFun(nd:SiteMapNode,enterOrOut:Boolean){
		//trace(nd.NodeName+"|"+nd.NodeURL);
		if(enterOrOut){
			_root.tips.txt=nd.NodeName;
			_root.tips.ifshow=true;
		}else{
			_root.tips.ifshow=false;
		}
	}
	public function MoveTree(x:Number,y:Number){
		if(this.$rootnode==undefined) return;
		this.$rootnode.Node._x=x;
		this.$rootnode.Node._y=y;
		this.LayoutSiteMap();
		this.DrawLine();
	}
	public function DrawLine(nd:SiteMapNode){
		if(nd==undefined){
			nd=this.$rootnode;
			this.SiteLineStyle();
			this.$sitelineMc.clear();
		}
		nd.DrawLineToChild();
		
	}
	public function SiteLineStyle(){
		//Say("set site line style.["+this.$sitelineMc+"]")
		this.$sitelineMc.lineStyle(3,0x999999,90);
	}
	
	public function LayoutSiteMap(nd:SiteMapNode){
		if(nd==undefined){
			nd=this.$rootnode;
		}
		//trace(nd.NodeName+": -----"+nd.Child.length)
		if(nd.Child.length>0){
			var nw:Number=nd.getWidthWithChild();
			var nwtemp:Number=nd.Child[0].getWidthWithChild();
			//trace(nw+"==="+nwtemp);
			
			nd.Child[0].Node._x=nd.Node._x-nw/2+nwtemp/2;
			nd.Child[0].Node._y=nd.Node._y+nd.Node._height/2+nd.Child[0].Node._height/2+this.$nodeYOffset;
			LayoutSiteMap(nd.Child[0]);
			for(var i=1;i<nd.Child.length;i++){
				nwtemp+=this.$nodeXOffset;
				var nwtemp2:Number=nd.Child[i].getWidthWithChild();
				nd.Child[i].Node._x=nd.Node._x-nw/2+nwtemp+nwtemp2/2;
				nd.Child[i].Node._y=nd.Child[0].Node._y;
				nwtemp+=nwtemp2;
				LayoutSiteMap(nd.Child[i]);
			}
		}
	}

	private function check():Boolean{
		if(this.checkstringEorN(this.$nodeLink) && this.checkstringEorN(this.$delLink) && this.checkstringEorN(this.$editLink) && this.checkstringEorN(this.$addChildLink)){
			return true;
		}else{
			return false;
		}
	}
	private function checkstringEorN(str:String):Boolean{
		if(str==undefined || str==null || str=="") 
		return false;
		else
		return true;
	}
	private function Say(o:Object){
		trace("sitMap: "+o.toString());
	}
	
}